package com.example.dragcontentviewdemo.helper.internal.animationlistener;

import android.animation.ValueAnimator;
import android.graphics.drawable.Drawable;
import android.view.View;

import com.example.dragcontentviewdemo.helper.internal.connector.AttachInfo;
import com.example.dragcontentviewdemo.helper.internal.util.CaculatorUtil;

/**
 * 视图在移动向右移动出视图边界时的过程动画监听器，用于调整背景层的透明度
 * 以及向外部传递位移量，与位移方向的参数
 */
public class MoveToRightAnimationUpdateListener implements ValueAnimator.AnimatorUpdateListener {

    private AttachInfo mAttachInfo;
    private View mView;
    private float mCurrAlpha;

    public MoveToRightAnimationUpdateListener(View viewOfAnimation, AttachInfo info){
        mView = viewOfAnimation;
        mAttachInfo = info;
    }

    public void updateCurrAlpha(){
        mCurrAlpha = mAttachInfo.mDecorView.getBackground().getAlpha();
    }

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        if(mAttachInfo.callback != null){
            float fraction = CaculatorUtil.calculateTranslationXFraction(mView);
            mAttachInfo.callback.onMoved(fraction,mAttachInfo.mDirection);
        }

        //跟随动画调整透明度
        //根据fraction调整透明度, fraction = 1 时应该完全透明
        int alpha = (int) (mCurrAlpha * (1 - animation.getAnimatedFraction()));

        //mAttachInfo.mDecorView.setBackground(new DraggableActivityHelper.CustomBackgroundDrawable(alpha));
        Drawable background = mAttachInfo.mDecorView.getBackground();
        background.setAlpha(alpha);
        mAttachInfo.mDecorView.invalidateDrawable(background);
    }


}